WIDTH = 250
HEIGHT = 250

p_n=1
n=0

def Draw():
    rc = Rect((125,125), (50,50))
    clr = (0,200,0)
    screen.draw.filled_rect(rc, clr)
def draw():
    screen.clear()
    Draw()
def on_mouse_down(pos):
    if pos[0] > 75 and pos[0] < 175 and pos[1] > 75 and pos[1] < 175:
        global p_n,n
        un = 50
        rc_l = []
        f = False
        n=p_n
        row = 5
        col = 5
    
        mygraph = [[-1 for col in range(cols)] for row in range(rows)]  
    
        re = []
        while n >= 0 and n != 100:
            if solving(mygraph, result)==False:
                print("no more")
                num=100
                return;
            if p_n - n == p_n:
                print(result)
            n -= 1
        p_n += 1

def update():
    pass

def draw():
    pass
def make_gray(graph, x, y, oldvalue, newvalue):
    for i in range(cols):
        if graph[x][i] == oldvalue:
            graph[x][i] = newvalue 

        if graph[i][y] == oldvalue:
            graph[i][y] = newvalue

        slash(graph, x - 1, y - 1, -1, -1, oldvalue, newvalue)
        slash(graph, x + 1, y - 1, 1, -1, oldvalue, newvalue)
        slash(graph, x - 1, y + 1, -1, -1, oldvalue, newvalue)
        slash(graph, x + 1, y + 1, 1, 1, oldvalue, newvalue)   

def slash(graph, x, y, x_increment, y_increment, oldvalue, newvalue):
    xx = x 
    yy = y
    while -1 < xx < rows and -1 < yy < cols:
        if graph[xx][yy] == oldvalue:
            graph[xx][yy] = newvalue

        xx += x_increment
        yy += y_increment

def pop_row_and_col(graph, result, rows, cols):        
    while True:
        if len(result) == 0:
            return -1, -1

        item = result.pop()
        make_gray(graph, item[0], item[1], len(result), -1)
        row_iter = item[0]
        col_iter = item[1] + 1

        while col_iter < cols:
            if  graph[row_iter][col_iter] == -1:
                return row_iter, col_iter

            col_iter += 1

def solving(graph, result):
    row_iter = 0
    col_iter = 0

    if len(result) > 0:
        row_iter, col_iter = pop_row_and_col(graph, result, rows, cols)
        if row_iter == -1 and col_iter == -1:
            return False

    found = False
    while True:    
        while True:
            if graph[row_iter][col_iter] == -1:
                make_gray(graph, row_iter, col_iter, -1, len(result))
                result.append((row_iter, col_iter))
                if len(result) == rows:
                    found = True
                    break

                row_iter += 1
                col_iter = 0
            else:
                col_iter += 1
                if col_iter >= cols:
                    row_iter, col_iter = pop_row_and_col(graph, result, rows, cols)
                    if row_iter == -1 and col_iter == -1:
                        break

        if found == False:
            row_iter, col_iter = pop_row_and_col(graph, result, rows, cols)
            if row_iter == -1 and col_iter == -1:
                break
        else:
            break

    return found


unit = 50
rc_list = []
finished = False
rows = 5
cols = 5
mygraph = [[-1 for col in range(cols)] for row in range(rows)]  
result = []



    

